'use strict';

exports.__esModule = true;
exports.asyncComponent = exports.ConnectedRouter = exports.browserHistory = undefined;

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

exports.default = createApp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _reactRedux = require('react-redux');

var _createBrowserHistory = require('history/createBrowserHistory');

var _createBrowserHistory2 = _interopRequireDefault(_createBrowserHistory);

var _reactRouterRedux = require('react-router-redux');

var _configStore = require('./configStore');

var _configStore2 = _interopRequireDefault(_configStore);

var _reducers = require('./reducers');

var _root = require('./root');

var _root2 = _interopRequireDefault(_root);

var _asyncComponent2 = require('./components/asyncComponent');

var _asyncComponent3 = _interopRequireDefault(_asyncComponent2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* eslint-disable no-shadow */
var browserHistory = (0, _createBrowserHistory2.default)(); // eslint-disable-line import/no-mutable-exports

function createApp() {
  var initReducers = {};
  var store = null;
  var currentView = null;
  var currentReducer = null;
  var currentMiddlewares = []; // Create a browser history by default

  function view(view) {
    currentView = view;

    return this;
  }

  function reducer() {
    var reducers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    // If passed with {"reducer1": reducer1, "reducer2": reducer2, ...}
    // Then combine the `system` reducer
    if ((typeof reducers === 'undefined' ? 'undefined' : (0, _typeof3.default)(reducers)) === 'object') {
      currentReducer = (0, _reducers.createReducer)(reducers);
      initReducers = reducers;
    } else {
      currentReducer = reducers;
    }

    return this;
  }

  function middleware(middlewares) {
    currentMiddlewares = middlewares;

    return this;
  }

  function render(container, callback) {
    _reactDom2.default.render(_react2.default.createElement(
      _reactRedux.Provider,
      { store: store },
      _react2.default.createElement(_root2.default, { view: currentView, store: store })
    ), container, callback);
  }

  function run(container, callback) {
    if (typeof container === 'string') {
      container = document.querySelector(container); // eslint-disable-line  no-param-reassign
    }

    if (!currentReducer) {
      this.reducer();
    }

    store = (0, _configStore2.default)(currentReducer, browserHistory, currentMiddlewares);
    store.asyncReducers = initReducers;

    render(container, callback);

    return this;
  }

  function createHistory(options) {
    exports.browserHistory = browserHistory = (0, _createBrowserHistory2.default)(options);

    return this;
  }

  function getStore() {
    return store;
  }

  return {
    view: view,
    reducer: reducer,
    middleware: middleware,
    run: run,
    createHistory: createHistory,
    getStore: getStore
  };
}

exports.browserHistory = browserHistory;
exports.ConnectedRouter = _reactRouterRedux.ConnectedRouter;
exports.asyncComponent = _asyncComponent3.default;